package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.rank.YRankMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_UNION_TRADE_EXP_CHANGE_AND_ACTIVITY,desc = "")
public class CrossTradeAddExNotifyCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CrossUnionProto.UniontTradeAddExpNotifyMsg msg = CrossUnionProto.UniontTradeAddExpNotifyMsg.parseFrom(packet.getBytes());
        CrossUnionInfo unionInfo = CrossUnionPb.parseCrossUnionInfo(msg.getUnion());
        CrossUnionMgr.updateUnionInfo(unionInfo);
        try {
            List<ActivityInfo> tempList = NormalActivityMgr.getUnionRiseRankActivityList();
            List<ActivityInfo> activityInfoList = new ArrayList<>();
            for (ActivityInfo activityInfo : tempList) {
                if ((activityInfo.getType() == eActivityType.UnionRankActivity.getValue() && activityInfo.getChildType() == 2)) {
                    activityInfoList.add(activityInfo);
                }
            }
            if (activityInfoList.size() > 0) {
                if (activityInfoList.size() > 1) {
                    CrossUnionMgr.getLogger().error("one more union rank activity exist.");
                }
                ActivityInfo activityInfo = activityInfoList.get(0);
                int activityId = activityInfo.getActivityId();

                UnionActivityMgr.addUnionActivityValue(activityId, unionInfo.getUnionUid(), unionInfo.getRankAddExp()); //排行
            }
        } catch (Exception e) {
            CrossUnionMgr.getLogger().error(e);
        }


        try {
            if (unionInfo != null) {
                List<ActivityInfo> tempList = NormalActivityMgr.getUnionRiseRankActivityList();
                List<ActivityInfo> activityInfoList = new ArrayList<>();
                for (ActivityInfo activityInfo : tempList) {
                    if (ActivityMgr.activityInTime(activityInfo) && ( activityInfo.getType() == eActivityType.CrossUnionActivity.getValue() &&  activityInfo.getChildType() == 3) ) {
                        activityInfoList.add(activityInfo);
                    }
                }
                for (ActivityInfo info : activityInfoList) {
                    if(info.getActivityId() == unionInfo.getRankActivityId()) {
                        /*
                        CrossUnionActivityRankDataProto.CrossAddUnionTradeExpReqMsg.Builder req = CrossUnionActivityRankDataProto.CrossAddUnionTradeExpReqMsg.newBuilder();
                        req.setActivityId(info.getActivityId());
                        req.setUnionId(unionInfo.getUnionUid());
                        req.setAddValue(unionInfo.getRankAddExp());
                        req.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_UNION_ACTIVITY_ADD_TRADE_EXP, req);
                        GamePlayerMgr.sendChannelPacket(0, message);
                        */

                        // 商会分组活动走这边
                        YRankMgr.uploadUnionYRank(
                                info.getActivityId(),
                                String.valueOf(eGamePlayerEventType.UnionExpRiseRank.getValue()),
                                unionInfo.getUnionUid(),
                                String.valueOf(unionInfo.getRankAddExp())
                        );
                    }
                }
            }
        } catch (Exception e) {
            CrossUnionMgr.getLogger().error("add trade exp error.", e);
        }

    }
}
